#include "GreyImageWrapper.h"
#include "AmaranthusCommon.h"
#include "ConvertNativeImageToGrey.h"

#include "itkImage.h"
#include "itkImageBase.h"
#include "itkImageIOBase.h"
#include "itkImageToVTKImageFilter.h"

#include "vtkSmartPointer.h"
#include "vtkPointData.h"

#include <QMessageBox>
#include <QString>

#include "SlicerWidget.h"

GreyImageWrapper::GreyImageWrapper()
{
    m_MinMaxCalc = MinMaxCalcType::New();
    m_ShiftFilter = ShiftFilterType::New();
    m_GreyImage = GreyImageType::New();
}

void
GreyImageWrapper::SetImage(ImageBaseType* imageBase)
{
    itk::ImageIOBase::IOComponentType type = m_ImageProperties->pixelType;
    switch(type)
    {
    case itk::ImageIOBase::UCHAR:  CastImageToGrey<unsigned char>(imageBase);        break;
    case itk::ImageIOBase::CHAR:   CastImageToGrey<signed char>(imageBase);          break;
    case itk::ImageIOBase::USHORT: CastImageToGrey<unsigned short>(imageBase);       break;
    case itk::ImageIOBase::SHORT:  CastImageToGrey<signed short>(imageBase);         break;
    case itk::ImageIOBase::UINT:   CastImageToGrey<unsigned int>(imageBase);         break;
    case itk::ImageIOBase::INT:    CastImageToGrey<signed int>(imageBase);           break;
    case itk::ImageIOBase::ULONG:  CastImageToGrey<unsigned long>(imageBase);        break;
    case itk::ImageIOBase::LONG:   CastImageToGrey<signed long>(imageBase);          break;
    case itk::ImageIOBase::FLOAT:  CastImageToGrey<float>(imageBase);                break;
//  case itk::ImageIOBase::DOUBLE: CastImageToGrey<double>(newImage);               break;
    default:
        (new QErrorMessage())->showMessage("Unknown Pixel Type when reading image");
    }

  /*  m_ShiftFilter->SetInput(greyImage);
    m_ShiftFilter->SetShift(0);
    m_ShiftFilter->SetScale(1);
    m_ShiftFilter->Update();*/

    m_MinMaxCalc->SetImage(m_GreyImage);
    m_MinMaxCalc->Compute();
    m_ImageProperties->range << m_MinMaxCalc->GetMinimum();
    m_ImageProperties->range << m_MinMaxCalc->GetMaximum();

    m_Initialized = true;
}

template<class TPixel>
void
GreyImageWrapper::CastImageToGrey(ImageBaseType* inputImage)
{
    typedef itk::Image<TPixel,3> NativeImageType;
    ConvertNativeImageToGrey converter;

    m_GreyImage = converter(inputImage, m_ImageProperties->pixelType);
}


